列をExcel VBAループして、空白以外をコピーして他の3つの列に貼り付けますか? (Excel VBA Loop through Column to Copy and Paste Non Blanks to 3 other Columns?)


問題の説明

列をExcel VBAループして、空白以外をコピーして他の3つの列に貼り付けますか? (Excel VBA Loop through Column to Copy and Paste Non Blanks to 3 other Columns?)

私は Excel VBA を初めて使用し、障害に遭遇しました。これを実行してもエラーは発生せず、どこから始めればよいかわかりません...

列 C を検索し、その値を列 (D、 G、I) 同じワークシートの。行 4 で検索を開始します。

コードをステップ実行してみましたが、エラーは currVal = ws.Cells(i, "c") にあると確信しています。ステップスルーしても、Excel シートでは何も起こりません (セルはコピーまたは貼り付けされません)。

どんな支援も大歓迎です! 私は午前中ずっとこれを見ていました。

Sub Copy()

Dim lastC As Integer
Dim i As Integer
Dim j As Integer
Dim currVal As String
Dim ws As Worksheet

Set ws = ActiveSheet
lastC = ws.Cells(Rows.Count, 1).End(xlUp).Row

For i = 4 To lastC
currVal = ws.Cells(i, "c")
   If ws.Cells(i, 1).Value <> "" Then
       currVal = ws.Cells(i, "D")
       currVal = ws.Cells(i, "G")
       currVal = ws.Cells(i, "I")
    End If

Next i

End Sub

実際の結果は何も起こらず (少なくとも私が見ることはできます)、エラーもなく、探し始めるところです!

期待される結果は、これを 3 つの追加の列にコピーすることです。結果は実際の式です。数式は、コピーする必要があるものです。

5/27/2019 9:39 ‑‑‑ 値

='15_2019‑02_003.CSV'!$A$2 ‑‑ 式

持っています値が元の CSV ファイルから取得されると、CSV ファイルへの参照を更新および削除する数式に対処しました。このコードの目的は、空白を検索してコピー/貼り付けすることだけです。Excel WS の画像

値が元の CSV ファイルから取得されると、CSV ファイルへの参照を更新および削除する数式に対処しました。このコードの目的は、空白を検索してコピー/貼り付けすることだけです。Excel WS の画像

値が元の CSV ファイルから取得されると、CSV ファイルへの参照を更新および削除する数式に対処しました。このコードの目的は、空白を検索してコピー/貼り付けすることだけです。Excel WS の画像


リファレンスソリューション

方法 1:

Adding to the comments, this is how I would have written that particular code, maybe it helps:

Sub Copy()

    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim lastC As Long: lastC = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Dim R As Long 'I like to use R (rows) & C (cols), just because makes it more readable (for me)

    For R = 4 To lastC
        With ws
            If .Cells(R, "A").Value <> "" Then
                .Cells(R, "D") = .Cells(R, "C").Value
                .Cells(R, "G") = .Cells(R, "C").Value
                .Cells(R, "I") = .Cells(R, "C").Value
            End If
        End With
    Next R

End Sub

PS: I prefer numbers instead of letters for .Cells reference, but in this case is understandable to use letter. Just for best practice ‑ readability wise, shouldn't mix them.

(by SadieFAB)

リファレンスドキュメント

  1. Excel VBA Loop through Column to Copy and Paste Non Blanks to 3 other Columns? (CC BY‑SA 2.5/3.0/4.0)

#vba #excel






関連する質問

2010カスタムリボンにアクセス (access 2010 custom ribbon)

セルに値が保存されているファイルを開く (Open files with values stored in cells)

コンソールはプログラミング言語で制御できますか? (Can consoles be controlled by programming languages?)

このコードを変更して、最後の行の下の行にデータを貼り付けるにはどうすればよいですか? (How do I modify this code to paste in the row under last row with data?)

Selenium-vba クラス名で要素を取得 (Selenium-vba Get element by Class Name)

セルからデータを抽出し、セルをアルファベット順に並べ替えるにはどうすればよいですか? (How do I extract data from a cell and order the cells alphabetically?)

VBAコードがセルをアクティブにしない (VBA code not activating cell)

列をExcel VBAループして、空白以外をコピーして他の3つの列に貼り付けますか? (Excel VBA Loop through Column to Copy and Paste Non Blanks to 3 other Columns?)

コードは 2 つの製品で動作しますが、3 番目の製品コードを追加すると、データが取得されますが、3 番目の製品だけに保存されません。どうしたの? (Code works with two products but when I add a third product code grabs data but doesn't save it only for third product. What's wrong with it?)

シート 2 の範囲内のセル名に基づいてシート (シート 1) を複製して名前を変更するために必要な VBA コード (VBA code needed to duplicate and rename a sheet (Sheet 1) based on cell names in a range on Sheet 2)

signtool.exe エラー: Excel マクロの署名時に SignerSign() が失敗しました (-2147220492/0x800403f4) (signtool.exe Error: SignerSign() failed (-2147220492/0x800403f4) when signing Excel Macro)

Excelで1つの列の「 - 」で区切られたデータを複数に分割する (Divide data separated from ' - ' in one column into more in excel)







コメント